from django.contrib import admin
from .models import Author, Genre, Book, BookInstance

# Define the admin class for Author
class AuthorAdmin(admin.ModelAdmin):
    list_display = ('last_name', 'first_name', 'date_of_birth', 'date_of_death')
    fields = ['first_name', 'last_name', ('date_of_birth', 'date_of_death')]  # 横向排列日期字段

# Register the admin class with the Author model
admin.site.register(Author, AuthorAdmin)

# Inline class to show BookInstances inside Book admin
class BooksInstanceInline(admin.TabularInline):
    model = BookInstance
    extra = 0  # 减少默认空行

# Register the Admin classes for Book using the decorator
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'display_genre')
    inlines = [BooksInstanceInline]  # 在书籍详情页显示关联的实例

# Register the Admin classes for BookInstance using the decorator
@admin.register(BookInstance)
class BookInstanceAdmin(admin.ModelAdmin):
    list_filter = ('status', 'due_back')
    fieldsets = (
        (None, {
            'fields': ('book', 'imprint', 'id')
        }),
        ('Availability', {
            'fields': ('status', 'due_back')
        }),
    )

# Register the Genre model
admin.site.register(Genre)